package sharetime.playlist.domain;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import sharetime.tag.domain.TaggedEntityObject;
import sharetime.user.domain.UserData;

@Entity
@NamedQueries( {
		@NamedQuery(name = "findHiddenPlaylist", query = "select p from Playlist p inner join p.owner as o where o.userId = :userId and p.hidden = true"),
		@NamedQuery(name = "findUnHiddenPlaylist", query = "select p from Playlist p inner join p.owner as o where o.userId = :userId and p.hidden = false"),
		@NamedQuery(name = "countTotalViews", query = "select sum(p.viewCount) from Playlist p")})
public class Playlist extends TaggedEntityObject {

	private static final long serialVersionUID = 7691926370058367855L;
	private String name;
	private String description;
	private double score;
	private int rating;

	private Integer derivativeCount;
	private Integer ancestorCount;
	private Integer viewCount;
	private Integer frontPagePosition;

	private boolean hidden = false;
	private boolean featured = false;
	private String documentIdentifier;
	
	private UserData owner;
	
	// The playlist that this was copied from
	private Playlist source;

	private Set<PlaylistResource> playlistsResources = new HashSet<PlaylistResource>();

	private Set<PlaylistResource> childPlaylistsResources = new HashSet<PlaylistResource>();

	private Set<Playlist> copiedPlaylists = new HashSet<Playlist>();

	private List<PlaylistComment> comments = new ArrayList<PlaylistComment>();

	private Set<PlaylistPermission> permissions = new HashSet<PlaylistPermission>();

	public Playlist() {
		// Initialize score and counters to 0.
		setScore(1.0);
		setRating(0);
		setDerivativeCount(new Integer(0));
		setViewCount(new Integer(0));
		setAncestorCount(new Integer(0));
	}

	@OneToMany(mappedBy = "playlist")
	public Set<PlaylistPermission> getPermissions() {
		return permissions;
	}

	public String getName() {
		return name;
	}

	@Column(length = 10485760)
	public String getDescription() {
		return description;
	}

	public String getDocumentIdentifier() {
		return documentIdentifier;
	}

	public double getScore() {
		return score;
	}

	public int getRating() {
		return rating;
	}

	public Integer getDerivativeCount() {
		return derivativeCount;
	}

	public void setDerivativeCount(Integer derivativeCount) {
		this.derivativeCount = derivativeCount;
	}

	public Integer getAncestorCount() {
		return ancestorCount;
	}

	public void setAncestorCount(Integer ancestorCount) {
		this.ancestorCount = ancestorCount;
	}

	public Integer getViewCount() {
		return viewCount;
	}

	public void setViewCount(Integer viewCount) {
		this.viewCount = viewCount;
	}

	public Integer getFrontPagePosition() {
		return frontPagePosition;
	}

	public void setFrontPagePosition(Integer frontPagePosition) {
		this.frontPagePosition = frontPagePosition;
	}

	public boolean isHidden() {
		return hidden;
	}

	public void setHidden(boolean isHidden) {
		this.hidden = isHidden;
	}

	public boolean isFeatured() {
		return featured;
	}

	public void setFeatured(boolean isFeatured) {
		this.featured = isFeatured;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "fk_owner_id")
	public UserData getOwner() {
		return owner;
	}

	// 从哪拷来的
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "fk_source_id")
	public Playlist getSource() {
		return source;
	}

	// 被哪些Playlist拷了
	@OneToMany(mappedBy = "source", fetch = FetchType.LAZY)
	public Set<Playlist> getCopiedPlaylists() {
		return copiedPlaylists;
	}

	@OneToMany(mappedBy = "playlist", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
	public Set<PlaylistResource> getPlaylistsResources() {
		return playlistsResources;
	}

	@OneToMany(mappedBy = "playlist", fetch = FetchType.LAZY)
	public Set<PlaylistResource> getChildPlaylistsResources() {
		return childPlaylistsResources;
	}

	@OneToMany(mappedBy = "playlist", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
	public List<PlaylistComment> getComments() {
		return comments;
	}

	public void setPermissions(Set<PlaylistPermission> permissions) {
		this.permissions = permissions;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setDocumentIdentifier(String documentIdentifier) {
		this.documentIdentifier = documentIdentifier;
	}

	public void setScore(double score) {
		this.score = score;
	}

	public void setRating(int rating) {
		this.rating = rating;
	}

	public void setOwner(UserData owner) {
		this.owner = owner;
	}

	public void setSource(Playlist source) {
		this.source = source;
	}

	public void setPlaylistsResources(Set<PlaylistResource> playlistsResources) {
		this.playlistsResources = playlistsResources;
	}

	public void setChildPlaylistsResources(
			Set<PlaylistResource> childPlaylistsResources) {
		this.childPlaylistsResources = childPlaylistsResources;
	}

	public void setCopiedPlaylists(Set<Playlist> copiedPlaylists) {
		this.copiedPlaylists = copiedPlaylists;
	}

	public void setComments(List<PlaylistComment> comments) {
		this.comments = comments;
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).append(
				"id", id).append("name", name).toString();
	}

}
